﻿//<TunynetCopyright>
//--------------------------------------------------------------
//<copyright>青岛拓宇网络科技有限公司 2005-2008</copyright>
//<version>V0.5</verion>
//<createdate>2008-4-24</createdate>
//<author>mazq</author>
//<email>mazq@tunynet.com</email>
//<log date="2008-4-24">创建</log>
//--------------------------------------------------------------
//</TunynetCopyright>

using System;
using System.Collections.Generic;
using System.Text;
using Tunynet.QingdaoCustoms.Caching;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;

namespace ChinaCustoms.Qingdao.Workflow.Supports
{

    /// <summary>
    /// 工作流管理平台用户角色
    /// </summary>
    public class WorkflowManagerRoles
    {
        private WorkflowManagerRoles()
        { }

        /// <summary>
        /// 获取工作流管理平台所有用户角色与应用程序对应关系
        /// </summary>
        public static Dictionary<string, List<string>> GetManagerRoles()
        {
            string cacheKey = "ManagerRoles";
            Dictionary<string, List<string>> managerRoles = null;

            managerRoles = WebCache.Get(cacheKey) as Dictionary<string, List<string>>;
            if (managerRoles == null)
            {
                managerRoles = BusinessDataProvider.Instance().GetManagerRoles();
                WebCache.Insert(cacheKey, managerRoles, WebCache.MinuteFactor * 10);
            }

            return managerRoles;
        }

        /// <summary>
        /// 获取用户可以管理的ApplicationCodeName
        /// </summary>
        /// <param name="user">IUser</param>
        /// <returns>ApplicationCodeName集合</returns>
        public static List<string> GetApplicationCodeNames(IUser user)
        {
            string cacheKey = string.Format("ApplicationCodeNamesForManager:{0}", user.LogOnName);

            List<string> applicationCodeNamesForManager = null;
            applicationCodeNamesForManager = WebCache.Get(cacheKey) as List<string>;

            if (applicationCodeNamesForManager == null)
            {
                applicationCodeNamesForManager = new List<string>();

                Dictionary<string, List<string>> managerRoles = GetManagerRoles();
                foreach (KeyValuePair<string, List<string>> item in managerRoles)
                {
                    RoleCollection rolesForApplication = user.Roles[item.Key];

                    if (rolesForApplication != null && rolesForApplication.Count > 0)
                    {
                        foreach (IRole role in rolesForApplication)
                        {
                            if (item.Value.Contains(role.CodeName))
                            {
                                applicationCodeNamesForManager.Add(item.Key);
                                break;
                            }
                        }
                    }
                }

                WebCache.Insert(cacheKey, applicationCodeNamesForManager, WebCache.MinuteFactor * 10);
            }

            return applicationCodeNamesForManager;
        }

    }
}
